package com.sikaryofficial.backend.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sikaryofficial.backend.domain.dto.req.information.ArticleEvaluateListReq;
import com.sikaryofficial.backend.domain.dto.req.information.ArticleEvaluateReq;
import com.sikaryofficial.backend.domain.dto.resp.information.ArticleEvaluateDTO;
import com.sikaryofficial.backend.domain.entity.ArticleEvaluate;
import com.sikaryofficial.backend.domain.mapping.ArticleEvaluateMapping;
import com.sikaryofficial.backend.mapper.ArticleEvaluateMapper;
import com.sikaryofficial.backend.service.IArticleEvaluateService;
import com.sikaryofficial.backend.service.IAttachmentService;
import com.sikaryofficial.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 文章评价Service业务层处理
 *
 * @author qinjinyuan
 * @date 2024-03-15
 */
@Service
public class ArticleEvaluateServiceImpl extends ServiceImpl<ArticleEvaluateMapper, ArticleEvaluate> implements IArticleEvaluateService {
    @Autowired
    private ArticleEvaluateMapper articleEvaluateMapper;
    @Autowired
    private IAttachmentService attachmentService;

    /**
     * 文章评价列表
     *
     * @param customerPage
     * @param req
     * @return
     */
    @Override
    public IPage<ArticleEvaluateDTO> evaluateList(Page<ArticleEvaluateDTO> customerPage, ArticleEvaluateListReq req) {
        return articleEvaluateMapper.evaluateList(customerPage, req,SecurityUtils.getUserId());
    }

    /**
     * 文章回复保存
     *
     * @param req
     * @return
     */
    @Override
    public ArticleEvaluateDTO saveEvaluate(ArticleEvaluateReq req) {
        Long articleEvaluateId = IdWorker.getId();
        Long userId = SecurityUtils.getUserId();
        ArticleEvaluate articleEvaluate = ArticleEvaluate
                .builder()
                .articleEvaluateId(articleEvaluateId)
                .articleId(req.getArticleId())
                .evaluateContent(req.getEvaluateContent())
                .createdTime(new Date())
                .createdName(SecurityUtils.getUsername())
                .createdBy(userId)
                .build();
        attachmentService.saveAttachmentList(articleEvaluateId, req.getAttachmentList(),
                SecurityUtils.getUserId());
        this.save(articleEvaluate);

        ArticleEvaluateDTO result = ArticleEvaluateMapping.INSTANCE.coverToDTO(articleEvaluate);
        result.setAttachmentList(req.getAttachmentList());
        return result;
    }
}
